package com.ftg.wymall.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ftg.wymall.admin.entity.UmsRole;
import com.ftg.wymall.admin.service.IUmsRoleService;
import com.ftg.wymall.comm.vo.CodeStatusEnum;
import com.ftg.wymall.comm.vo.RespMessUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author kangjx
 * @since 2023-10-27
 */
@RestController
@Api(tags = "角色管理")
@RequestMapping("/ums/admin/umsRole")
public class UmsRoleController {


    @Resource
    IUmsRoleService iUmsRoleService;

    /**
     * 分页方法
     *
     * @param pageSize  页码
     * @param pageLimit 显示条数
     * @param roleName  角色名称
     * @return RespMessUtil
     */
    @GetMapping("/showAllWithPage")
    public RespMessUtil<IPage> pageRole(
            @ApiParam(name = "pageSize", value = "页码", required = true) @RequestParam(value = "pageSize", defaultValue = "1") long pageSize,
            @ApiParam(name = "pageLimit", value = "显示条数", required = true) @RequestParam(value = "pageLimit", defaultValue = "10") long pageLimit,
            @ApiParam(name = "roleName", value = "角色名称") @RequestParam(value = "roleName", defaultValue = "") String roleName

    ) {
        return iUmsRoleService.showAllWithPage(pageSize, pageLimit, roleName);
    }

    /**
     * 添加角色
     *
     * @param umsRole UmsRole对象，包含要添加的角色信息
     * @return RespMessUtil对象，表示添加角色的响应结果
     */
    @PostMapping("/addRole")
    public RespMessUtil<Boolean> addRole(
            @RequestBody UmsRole umsRole
    ) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, iUmsRoleService.save(umsRole));
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/delRole/{id}")
    public RespMessUtil<Boolean> delRole(
            @ApiParam(name = "id", value = "主键", required = true) @PathVariable("id") Long id
    ) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, iUmsRoleService.removeById(id));
    }

    @ApiOperation("批量删除角色")
    @DeleteMapping("/delRoles")
    public RespMessUtil<Boolean> delRoles(
            @ApiParam(name = "ids", value = "主键", required = true) @RequestParam("ids") List<Long> ids
    ) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, iUmsRoleService.removeByIds(ids));
    }

    @ApiOperation("切换状态")
    @PutMapping("/switchStatus/{id}")
    public RespMessUtil<Boolean> switchStatus(
            @ApiParam(name = "id", value = "主键", required = true) @PathVariable("id") Long id
    ) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, iUmsRoleService.switchStatus(id));
    }

    @ApiOperation("修改角色")
    @PutMapping("/updateRole")
    public RespMessUtil<Boolean> updateRole(
            @RequestBody UmsRole umsRole
    ) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, iUmsRoleService.updateById(umsRole));
    }

}
